package com.credithc.customers.manager.service.impl;

import com.credithc.baseserv.core.service.impl.BaseServiceImpl;
import com.credithc.baseserv.core.utils.DateTimeUtils;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.customers.base.codes.CustomerCodes;
import com.credithc.customers.base.exception.CustomerException;
import com.credithc.customers.base.po.UnifySmsAccountPo;
import com.credithc.customers.base.utils.ClassUtils;
import com.credithc.customers.manager.ro.SmsAccountRo;
import com.credithc.customers.manager.service.SmsAccountService;
import com.credithc.customers.manager.vo.SmsAccountVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: 业务监控处理
 * @Author: fby
 * @Date: 2020/03/24 10:21
 * @Description: com.credithc.customers.manager.service.impl
 * @Version: V1.0.0
 */
@Service
@Slf4j
public class SmsAccountServiceImpl extends BaseServiceImpl<UnifySmsAccountPo, SmsAccountRo, SmsAccountVo> implements SmsAccountService {

    @Override
    public PageInfo<SmsAccountVo> getSmsAccount(SmsAccountRo ro) {
        try {
            Example example = new Example(UnifySmsAccountPo.class);
            Example.Criteria criteria = example.createCriteria();
            example.setOrderByClause("create_time desc");
            if (StringUtils.isNotEmpty(ro.getSmsAccount())) {
                criteria.andEqualTo("smsAccount", ro.getSmsAccount());
            }
            if (StringUtils.isNotEmpty(ro.getSmsTemplateId())) {
                criteria.andEqualTo("smsTemplateId", ro.getSmsTemplateId());
            }
            if (StringUtils.isNotEmpty(ro.getSystemSign())) {
                criteria.andEqualTo("systemSign", ro.getSystemSign());
            }
            if (StringUtils.isNotEmpty(ro.getSubSystemSign())) {
                criteria.andEqualTo("subSystemSign", ro.getSubSystemSign());
            }
            PageInfo<UnifySmsAccountPo> pageInfo = this.queryPageResultByExample(example, ro.getPageNo(), ro.getPageSize());
            List<SmsAccountVo> vos = new ArrayList<>();
            if (pageInfo.getTotalCount() > 0) {
                for (UnifySmsAccountPo po : pageInfo.getList()) {
                    SmsAccountVo vo = new SmsAccountVo();
                    BeanUtils.copyProperties(po, vo);
                    if (po.getCreateTime() != null) {
                        vo.setCreateTime(DateTimeUtils.dateToStrLong(po.getCreateTime()));
                    }
                    //处理状态
                    if (po.getStatus() != null) {
                        if (po.getStatus() == 1) {
                            vo.setStatus("启用");
                        } else {
                            vo.setStatus("停用");
                        }
                    }
                    vos.add(vo);
                }
            }
            return new PageInfo<>(vos, pageInfo.getTotalCount(), ro.getPageNo(), ro.getPageSize());
        } catch (DataAccessException e) {
            log.error("查询短信账户报错:" + ClassUtils.getClassName() + "--" + ClassUtils.getMethodName() + "--" + e.getMessage());
            throw new CustomerException(CustomerCodes.DATABASE_ERROR_QUERY);
        }
    }

    @Override
    public void updateSmsAccount(SmsAccountRo ro, Integer id) {
        try {
            UnifySmsAccountPo po = new UnifySmsAccountPo();
            po.setId(id.toString());
            if (StringUtils.isNotEmpty(ro.getSmsAccount())) {
                po.setSmsAccount(ro.getSmsAccount());
            }
            if (StringUtils.isNotEmpty(ro.getSmsTemplateId())) {
                po.setSmsTemplateId(ro.getSmsTemplateId());
            }
            if (StringUtils.isNotEmpty(ro.getSystemSign())) {
                po.setSystemSign(ro.getSystemSign());
            }
            if (StringUtils.isNotEmpty(ro.getSubSystemSign())) {
                po.setSubSystemSign(ro.getSubSystemSign());
            }
            if (StringUtils.isNotEmpty(ro.getTemplateType())) {
                po.setTemplateType(ro.getTemplateType());
            }
            po.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            this.updateByPrimaryKeySelective(po);
        } catch (Exception e) {
            log.error("修改短信账户报错:{}", e.getMessage());
            throw new CustomerException(CustomerCodes.DATABASE_ERROR_UPDATE);
        }
    }

    @Override
    public void remove(Integer id) {
        this.deleteById(id.toString());
    }

    @Override
    public void addSmsAccount(SmsAccountRo ro) {
        try {
            UnifySmsAccountPo po = new UnifySmsAccountPo();
            BeanUtils.copyProperties(ro, po);
            po.setCreateTime(new Timestamp(System.currentTimeMillis()));
            if (ro.getStatus() == null) {
                po.setStatus(1);
            }
            this.save(po);
        } catch (DataAccessException e) {
            log.error("新增短信账户报错:{}", e.getMessage());
            throw new CustomerException(CustomerCodes.DATABASE_ERROR_INSERT);
        }
    }


}
